<?php
// WARNING: this will work only in PHP5
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
require_once(DOKU_INC.'phpincludes/ACC_Form_Base.php');

/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Stephane Chamberland <dokuwiki@meteokid.ca>
 * @date       2009-01-22
 */
class ACC_Form_KF_cancel extends ACC_Form_Base {

    var $mailTo = 'keenereserve@accmontreal.ca';
    var $mailToAllowed = array('keenereserve@accmontreal.ca');
    //var $mailTo = 'web@accmontreal.ca';
    //var $mailToAllowed = array('web@accmontreal.ca');
    var $mailToField = 'to_email';
    var $mailFromField = 'from_email';
    var $mailFromNameField = 'from_name';
    var $mailSubjectPrefix = '[KF reserv] ';

    var $mailSubjectFmtName = 'mailSubjectFmt';
    var $mailBodyFmtName = 'mailBodyFmt';

    var $metaFileSuffix = 'keene';
    var $metaFileid = '__keene';

    var $langArray = array(
        'fr' => array(
            'mailSent'       => 'Votre message a &eacute;t&eacute; envoy&eacute;',
            'mailSentError'  => "Erreur d'envoie, SVP contacter l'administrateur a ",
            'formDescFmt' => 'name keenefarm_cancel
action metafile keene
Thanks "Votre annulation a bien ete notee. Nous esperons vous revoir sous peu."
Fieldset "Keene Farm - Annulation"
const  reserv cancellation
static "SVP fournir les details suivant pour que nous puisssions retrouver votre reservation (le courriel fourni doit etre le meme que celui utilise lors de votre reservation)."
Textbox "Numero de Confirmation/Reservation" #80 &uid
email "Courriel" #80 &from_email
textarea "Questions/Commentaires?" ! x3 #5000 &msgc
submit "Soumettre votre Annulation" ',
            'mailSubjectFmt' => '__cabin__ __reserv__ for __from_name__ on __date__ for __nights__ nights, __people__ people (__people_member__ members, __people_guest__ guests, __people_children__ children)',
            'mailBodyFmt' => 'Keene Farm __reserv__ request
Submited by __from_name__ <__from_email__>
==== Details ====
__ACC_Form_list_all__
=============',
            'error_no_match' => 'Nous ne pouvons trouver de reservation correspondant au courriel/numero que vous avez fourni. SVP verifier et essayer de nouveau.',
            'msg_cancel'    => '
<b>Votre reservation pour le  __cabin__ a Keene Farmhas a ete annulee.</b>.<br />
<em>Nous esperons vous revoir sous peu</em>.<br />
<br />
Details de la reservation qui a ete annulee:<br />
Reservation pour le __cabin__ a Keene Farm le __date__ pour __nights__ nuit(s), __people_member__ membres, __people_guest__ non-membres, __people_children__ enfants<br />
Faite par __from_name__ le __now__<br />'

        ),
        'en' => array(
            'mailSent'       => 'Your message was sent sucessfully',
            'mailSentError'  => 'There was an error sending your email, please contact the admin at ',
            'formDescFmt' => 'name keenefarm_cancel
action metafile keene
Thanks "Your cancellation have been registered. Hope we see you back soon."
Fieldset "Keene Farm Cancellation"
const  reserv cancellation
static "Please provide us with these details so we can find the reservation to cancel (the email address should be the same as the one used for your reservation)."
Textbox "Confirmation/Reservation number" #80 &uid
email "E-Mail" #80 &from_email
textarea "Questions/Comments?" ! x3 #5000 &msgc
submit "Submit your Cancellation" ',
            'mailSubjectFmt' => '__cabin__ __reserv__ for __from_name__ on __date__ for __nights__ nights, __people__ people (__people_member__ members, __people_guest__ guests, __people_children__ children)',
            'mailBodyFmt' => 'Keene Farm __reserv__ request
Submited by __from_name__ <__from_email__>
==== Details ====
__ACC_Form_list_all__
=============',
            'error_no_match' => 'We were not able to find a request matching this number/email combination. Please check and try again.',
            'msg_cancel'    => '
<b>Your reservation for Keene Farm __cabin__ has been canceled</b>.<br />
<em>We hope to see you back soon</em>.<br />
<br />
Details of the canceled reservation were:<br />
Reservation at Keene Farm __cabin__ on __date__ for __nights__ night(s), __people_member__ members, __people_guest__ guests, __people_children__ children<br />
Made by __from_name__ on __now__<br />'
        )
    );

    var $cancelFields = NULL;
    var $metaFile = NULL;

    function _postcheckpost($data) {
        $errors = array();
        $values = $this->buro->_collectpost($data);

        $metaFile = $this->_metaFile_get($this->metaFileid);
        $searchOk = $this->_metaFile_search($metaFile,array('from_email'=>$values['from_email']),$values['uid']);
        if (!count($searchOk)) {
            $errors['from_email'] = 1;
            $errors['uid'] = 1;
            msg($this->getLang('error_no_match'),-1);
        } else {
            //unset($metaFile[$values['uid']]);
            $metaFile[$values['uid']]['status'] = 'canceled';
            $metaFile[$values['uid']]['msgc'] = $values['msgc'];
            $metaFile[$values['uid']]['nowc'] = date('c');
            $this->cancelFields = $metaFile[$values['uid']];
            $this->metaFile = $metaFile;
            //TODO: if was custodian and in less than 3d, may add in black list of custodian + reflect on other reservations for these dates
        }

        return $errors;
    }

    function _action_ok($data,$thanks) {
        if (!is_null($this->metaFile)) $ok = $this->_metaFile_save($this->metaFile,$this->metaFileid);

        $people = 0;
        foreach(array('people_member','people_guests','people_children') as $k) {
            if (is_numeric($this->cancelFields[$k])) $people += $this->cancelFields[$k];
        }

        $values = $this->buro->_collectpost($data);
        $this->cancelFields['people'] = $people;
        $this->cancelFields['msgc'] = $values['msgc'];
        $this->cancelFields['reserv'] = $values['reserv'];
        $ok = $this->_sendMail($this->cancelFields);

        if ($ok) {
            $sr = array();
            foreach($this->cancelFields as $k=>$v) $sr['__'.$k.'__'] = $v;
            return str_replace(array_keys($sr),array_values($sr),$this->getLang('msg_cancel'));
        }
        msg($this->getLang('mailSentError'));
        return false;
    }

}

$my_contact_form = new ACC_Form_KF_cancel($this);
print($my_contact_form->_action_show_form());
